package com.xbang.attendance.web.action.report.productsales;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.helper.order.DeliveryOrderReport;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

public class Search extends SearchActionSupport {

	private static final Log logger = LogFactory.getLog(Search.class);
	
	@Autowired
	private SystemService systemService;
	
	private List<DeliveryOrderReport> deliveryOrderReports = new ArrayList<DeliveryOrderReport>();
	
	public String search() throws Exception {

		return "search";
	}
	
	@Action("/report/productsales/search/do")
	public String doSearch() throws Exception {
		String groupHQLSentence = "SELECT detail.product.id,detail.product.productCode,detail.product.productName, SUM(detail.productNumber) FROM DeliveryDetails detail WHERE 1 = 1 ";

		SearchComponent tmpSearchComponent = new SearchComponent();
		tmpSearchComponent.getParameters().putAll(searchComponent.getParameters());
		tmpSearchComponent.getWhereSentences().putAll(searchComponent.getWhereSentences());
		tmpSearchComponent.addSearchElement("groupby.detail.product.id", null);

		Result tmpResult = systemService.searchByPage(groupHQLSentence, tmpSearchComponent, page);
		// /////
		List<Object[]> lists = tmpResult.getItems();
		page = tmpResult.getPage();
		if (lists == null || lists.isEmpty()) {
			return "index";
		}

		for (Object[] objects : lists) {
			DeliveryOrderReport deliveryOrderReport = new DeliveryOrderReport();
			deliveryOrderReports.add(deliveryOrderReport.copyPerperties(null, null, null, new Product().copyProperties((Integer) objects[0], objects[2]
					.toString(), objects[1].toString()), (BigDecimal) objects[3], false, false, true));
			continue;
		}

		return "index";
	}
	
	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public List<DeliveryOrderReport> getDeliveryOrderReports() {
		return deliveryOrderReports;
	}

	public void setDeliveryOrderReports(
			List<DeliveryOrderReport> deliveryOrderReports) {
		this.deliveryOrderReports = deliveryOrderReports;
	}
	
}
